package com.uzykj.modules.front.web;

import com.uzykj.common.json.AjaxJson;
import com.uzykj.common.utils.CacheUtils;
import com.uzykj.core.persistence.Page;
import com.uzykj.core.web.BaseController;
import com.uzykj.modules.blog.entity.BlogCategory;
import com.uzykj.modules.blog.entity.BlogNote;
import com.uzykj.modules.blog.entity.BlogReview;
import com.uzykj.modules.blog.service.BlogNoteService;
import com.uzykj.modules.blog.service.BlogReviewService;
import com.uzykj.modules.front.dto.ReviewDto;
import com.uzykj.modules.sys.entity.User;
import com.uzykj.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.HtmlUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author ghostxbh
 * @date 2020/7/24
 * @description 攻略文章前台
 */
@Controller
@RequestMapping(value = "${frontPath}/n")
public class NoteController extends BaseController {
    @Autowired
    private BlogNoteService blogNoteService;

    @Autowired
    private BlogReviewService blogReviewService;

    @RequestMapping("{code}.html")
    public String note(@PathVariable String code, HttpServletRequest request,
                       HttpServletResponse response, Model model) {
        List<BlogCategory> categoryList = (List<BlogCategory>) CacheUtils.get(UserUtils.BLOG_CACHE, UserUtils.CACHE_BLOG_LIST);
        BlogNote blogNote = blogNoteService.getByCode(code);
//        blogNote.setContents(HtmlUtils.htmlEscape(blogNote.getContents()));

        Page<BlogReview> page = new Page<BlogReview>();
        page.setPageNo(0);
        page.setPageSize(10);
        BlogReview review = new BlogReview();
        review.setNote(new BlogNote(blogNote.getId()));
        Page<BlogReview> reviewPage = blogReviewService.findPage(page, review);

        blogNoteService.updateRead(blogNote.getId(), blogNote.getReadCount() + 1);

        Page<BlogNote> blogNotePage = new Page<BlogNote>(1, 5);
        blogNotePage.setOrderBy("a.read_count desc");

        BlogNote noteQuery = new BlogNote();
        noteQuery.setCreateBy(new User(blogNote.getCreateBy().getId()));
        Page<BlogNote> notePage = blogNoteService.findPage(blogNotePage, noteQuery);
        List<BlogNote> noteList = notePage.getList();

        Page<BlogNote> blogNotePage1 = new Page<BlogNote>(1, 5);
        blogNotePage1.setOrderBy("a.read_count desc");
        BlogNote noteQuery1 = new BlogNote();
        noteQuery1.setCity(blogNote.getCity());
        Page<BlogNote> notePage1 = blogNoteService.findPage(blogNotePage1, noteQuery1);
        List<BlogNote> cityNoteList = notePage1.getList();

        model.addAttribute("categoryList", categoryList);
        model.addAttribute("noteList", noteList);
        model.addAttribute("cityNoteList", cityNoteList);
        model.addAttribute("note", blogNote);
        model.addAttribute("reviewList", reviewPage.getList());
        return "modules/front/note";
    }

    @RequestMapping("review")
    @ResponseBody
    public AjaxJson review(@RequestParam("noteId") String noteId,
                           HttpServletRequest request, HttpServletResponse response) {
        AjaxJson j = new AjaxJson();
        try {
            Page<BlogReview> page = new Page<BlogReview>(request, response);
            BlogReview review = new BlogReview();
            review.setNote(new BlogNote(noteId));
            page = blogReviewService.findPage(page, review);
            j.setSuccess(true);
            j.setMsg("ok");
            j.put("reviewList", page);
        } catch (Exception e) {
            logger.error("page review error", e);
            j.setSuccess(false);
            j.setMsg("获取评论分页失败");
        }
        return j;
    }

    @RequestMapping(value = "review/add", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson addReview(@RequestBody() ReviewDto reviewDto,
                              HttpServletRequest request, HttpServletResponse response) {
        AjaxJson j = new AjaxJson();
        try {
            User user = UserUtils.getUser();
            if (user.getLoginName() == null) {
                j.setSuccess(false);
                j.setMsg("请登录后在执行操作");
                return j;
            }
            BlogReview review = new BlogReview();
            review.setNote(reviewDto.getNote());
            review.setContents(reviewDto.getContents());
            review.setReviewer(new User(user.getId()));
            blogReviewService.save(review);
            j.setSuccess(true);
            j.setMsg("ok");
        } catch (Exception e) {
            logger.error("add review error", e);
            j.setSuccess(false);
            j.setMsg("添加评论失败");
        }
        return j;
    }

    @RequestMapping(value = "like/{noteId}", method = RequestMethod.GET)
    @ResponseBody
    public AjaxJson addLike(@PathVariable String noteId,
                            HttpServletRequest request, HttpServletResponse response) {
        AjaxJson j = new AjaxJson();
        try {
            blogNoteService.updateLike(noteId);
            j.setSuccess(true);
            j.setMsg("ok");
        } catch (Exception e) {
            logger.error("add review error", e);
            j.setSuccess(false);
            j.setMsg("点赞失败");
        }
        return j;
    }
}
